package com.zfx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfx.pojo.User;
import com.zfx.service.IUserService;
import com.zfx.util.JsonMsg;
import com.zfx.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zfx
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, @RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();
        User u = userService.login(user);
        if(u != null) {
            request.getSession().setAttribute("userId", u.getUserId());
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
            jsonObject.setData("userId", u.getUserId());
            jsonObject.setData("username", u.getUsername());
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return objectMapper.writeValueAsString(jsonObject);
    }

    @RequestMapping("/loginOut")
    public JsonObject loginOut(HttpServletRequest request) {

        request.getSession().removeAttribute("userId");

        JsonObject jsonObject = new JsonObject();
        jsonObject.setCode("200");
        jsonObject.setMsg("success");

        return jsonObject;
    }

    @RequestMapping("/register")
    public String register(@RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();
        boolean exists = userService.register(user);
        if(exists) {
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return objectMapper.writeValueAsString(jsonObject);
    }

    @RequestMapping("/getInfo")
    public User findByUserId(HttpServletRequest request) throws JsonProcessingException {
        return userService.findById(String.valueOf(request.getSession().getAttribute("userId")));
    }

    @RequestMapping("/updateInfo")
    public String updateInfo(@RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();
        int update = userService.update(user);
        if(update == 1) {
            jsonObject.setMsg("success");
        } else {
            jsonObject.setMsg("error");
        }
        return objectMapper.writeValueAsString(jsonObject);
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(HttpServletRequest request, @RequestBody String jsonData) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();

        //解析json
        HashMap<String,String> dataMap = objectMapper.readValue(jsonData, HashMap.class);

        User user = userService.findById(String.valueOf(request.getSession().getAttribute("userId")));
        String newpwd = dataMap.get("newpwd");
        String oldpwd = dataMap.get("oldpwd");
        if(user == null || !oldpwd.equals(user.getPassword())) {
            jsonObject.setMsg("error");
        } else {
            user.setPassword(newpwd);
            userService.update(user);
            jsonObject.setMsg("success");
        }
        return objectMapper.writeValueAsString(jsonObject);
    }

}
